package br.urca.impl;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.HistoricoDAO;
import br.urca.model.Historico;

@Transactional
public class JPAHistoricoDAO implements HistoricoDAO {
	
	public static final String COD_DISCI_PRATICA_COMP_CURR = "EF119";
	
	private EntityManager manager;
	
	public JPAHistoricoDAO() {
		this(null);
	}
	
	@Inject
	public JPAHistoricoDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<Historico> todos(String matricula) {
		try {
			return this.manager
					.createQuery(
							"SELECT h FROM Historico h "
									+ "WHERE h.id.matricula = :matricula",
							Historico.class).setParameter("matricula", matricula)
					.getResultList();
		} catch(Exception e) {
			return null;
		}
	}

	@Override
	public List<Historico> getAtividadesComp(String matricula) {
		
		
		List<String> listaCodigosAtivComp = Arrays.asList("AT006", "CB124",
				"DD331", "FC094", "FC095", "FC096", "FC108", "FC109",
				"NB008"); 
		
		try {
			return this.manager
					.createQuery(
							"SELECT h FROM Historico h "
									+ "WHERE h.id.matricula = :matricula "
									+ "AND h.id.codDisciplina IN (:codigos) ",
							Historico.class)
					.setParameter("matricula", matricula)
					.setParameter("codigos", listaCodigosAtivComp)
					.getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public Historico getPraticasCompCurr(String matricula) {
		try {
			return this.manager
					.createQuery(
							"SELECT h FROM Historico h "
									+ "WHERE h.id.matricula = :matricula "
									+ "AND h.id.codDisciplina = :codigo ",
							Historico.class)
					.setParameter("matricula", matricula)
					.setParameter("codigo", COD_DISCI_PRATICA_COMP_CURR)
					.getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}
	

	@Override
	public int getCreditosAtividadesComp(String matricula) {
		List<String> listaCodigosAtivComp = Arrays.asList("FC089");
		int retorno = 0;
		List<Historico> mList;
		try {
			mList = this.manager
					.createQuery(
							"SELECT h FROM Historico h "
									+ "WHERE h.id.matricula = :matricula "
									+ "AND h.situacao.acao = :acao "
									+ "AND h.id.codDisciplina IN (:codigos) ",
							Historico.class)
					.setParameter("matricula", matricula)
					.setParameter("acao", "1")
					.setParameter("codigos", listaCodigosAtivComp)
					.getResultList();
			
			for(Historico h: mList) {
				if (h.getCreditos() > 0)
					retorno += h.getCreditos();
			}
			return retorno;
		} catch (Exception e) {
			return 0;
		}
	}

	@Override
	public List<Historico> getHistoricoParaCR(String matricula) {
		List<String> situacoes = Arrays.asList("AP", "AV","RN", "RF"); 
		
		try {
			return this.manager
					.createQuery(
							"SELECT h FROM Historico h "
									+ "WHERE h.id.matricula = :matricula "
									+ "AND h.situacao.codSituacao IN (:codigos) ",
							Historico.class)
					.setParameter("matricula", matricula)
					.setParameter("codigos", situacoes)
					.getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<Historico> getDiscParaHistorico(String matricula) {
		List<String> codigos = Arrays.asList("FC089", "EF119"); 
		try {
			return this.manager
					.createQuery(
							"SELECT h FROM Historico h "
									+ "WHERE h.id.matricula = :matricula "
									+ "AND h.id.codDisciplina not in (:codigos) "
									+ "AND h.situacao.codSituacao not in (:situacao) ",
							Historico.class)
					.setParameter("matricula", matricula)
					.setParameter("codigos", codigos)
					.setParameter("situacao", "").getResultList();
		} catch (Exception e) {
			return null;
		}
	}

}
